/*这里是登录注册页面的左右移动*/
var loginBtn=document.getElementsByClassName("loginBtn")[0];
var loginBtnContainer=document.getElementsByClassName("loginBtnContainer")[0];
var handoverLogin=document.getElementsByClassName("handoverLogin")[0];
var singBtnContainer=document.getElementsByClassName("singBtnContainer")[0];
var handoverSign=document.getElementsByClassName("handoverSign")[0];
var signBtn=document.getElementsByClassName("signBtn")[0];
loginBtn.onclick=function(){
    loginBtnContainer.style.left="100%";
    handoverSign.style.right="100%";
    setTimeout(function(){
        handoverLogin.style.left="0";
        singBtnContainer.style.right="0";
    },300);
}
signBtn.onclick=function(){
    handoverLogin.style.left="100%";
    singBtnContainer.style.right="100%";
    setTimeout(function(){
        loginBtnContainer.style.left="0";
        handoverSign.style.right="0";
    },300);
}
/*随机数做验证码*/
var comple=document.getElementsByClassName("comple")[0];
var random;
function num(){
    random=Math.floor(Math.random()*9000+1000);
    comple.innerHTML=random;
}
num();
comple.onclick=function(){
    num();
}
/*隐私协议*/
var privacyInput=document.getElementsByClassName("privacyInput")[0];
var sign_btn=document.getElementsByClassName("sign_btn")[0];
let signTrue=document.getElementsByClassName("signTrue")[0];
sign_btn.style.backgroundColor="#8d9299";
privacyInput.onclick=function(){
    if(privacyInput.checked==true){
        sign_btn.style.backgroundColor="";
        sign_btn.style.cursor="pointer";
        sign_btn.onclick=function(){
            if(signTrue.value!==comple.innerHTML){
                Tips("验证码不正确");
            }else if(signpassword[0].value!==signpassword[1].value){
                signpassword[0].style.border="1px solid red";
                signpassword[1].style.border="1px solid red";
                Tips("两次密码不一致");
            }else{
                Sign();
            }
        }
    }else{
        sign_btn.style.backgroundColor="#8d9299";
        sign_btn.style.cursor="no-drop";
        sign_btn.onclick=function(){

        }
    }
}

/*其他登录方式*/
let cutLogin=document.getElementsByClassName("cutLogin")[0];
let emailLogin=document.getElementsByClassName("emailLogin")[0];
let loginInputContainer=document.getElementsByClassName("loginInputContainer")[0];
let returnLoginEmail=document.getElementsByClassName("returnLoginEmail")[0];
cutLogin.onclick=function(){
    emailLogin.style.display="block";
    loginInputContainer.style.display="none";
}
returnLoginEmail.onclick=function(){
    emailLogin.style.display="none";
    loginInputContainer.style.display="block";
}
/*其他方式注册*/
let cut=document.getElementsByClassName("cut")[0];
let signEmail=document.getElementsByClassName("signEmail")[0];
let signInputContainer=document.getElementsByClassName("signInputContainer")[0];
let returnSignEmail=document.getElementsByClassName("returnSignEmail")[0];
cut.onclick=function(){
    signInputContainer.style.display="none";
    signEmail.style.display="block";
}
returnSignEmail.onclick=function(){
    signInputContainer.style.display="block";
    signEmail.style.display="none";
}
/*其他方式注册*/
/*邮箱输入框*/
let emailLoginInputText=document.getElementsByClassName("emailLoginInputText")[0];
/*点击获取验证码*/
let valiBtn=document.getElementsByClassName("valiBtn");
/*验证码输入框*/
let identifyingInput=document.getElementsByClassName("identifyingInput")[0];
/*密码*/
let identifyingPasswordInput=document.getElementsByClassName("identifyingPasswordInput");
let QQ = /^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
var email=null;
let emailLoginBtn=document.getElementsByClassName("emailLoginBtn");
valiBtn[1].onclick=function(){
    if(!QQ.test(emailLoginInputText.value)){
        Tips("邮箱格式不正确");
        emailLoginInputText.style.border="1px solid red";
    }else{
        email=emailLoginInputText.value;
        $.ajax({
            url:'../email/sendEmail',
            type:"post",
            data:{
                qqEmail:emailLoginInputText.value
            },
            success:function(data){
                /*在获取验证码成功后将邮箱输入框设置为不可选，不让其改变邮箱*/
                /*点击进行注册*/
                JudgeQQ();
            }
        });
    }
}
JudgeQQ();
function JudgeQQ(){
    emailLoginBtn[1].onclick=function(){
        if(emailLoginInputText.value===""){
            Tips("请输入QQ邮箱");
        }else
        if(identifyingInput.value===""){
            Tips("请输入验证码");
        }else
            /*判断密码是否为空*/
        if(identifyingPasswordInput[0]===""){
            Tips("密码不能为空");
            identifyingPasswordInput[0].style.border="1px solid red";
        }else
            /*判断二次密码是否为空*/
        if(identifyingPasswordInput[1]===""){
            Tips("第二次密码不能为空");
            identifyingPasswordInput[1].style.border="1px solid red";
        }else
        if(!(identifyingPasswordInput[0].value.length>=8&&identifyingPasswordInput[0].value.length<=12)){
            Tips("密码长度应为8~12位");
        }else
        if(identifyingPasswordInput[0].value!==identifyingPasswordInput[1].value){
            Tips("两次密码不一样");
            identifyingPasswordInput[0].style.border="1px solid red";
            identifyingPasswordInput[1].style.border="1px solid red";
        }else{
            OtherSign();
        }
    }
}
function OtherSign(){
    $.ajax({
        url:'../email/emailRegister',
        type:"post",
        data:{
            emailCode:identifyingInput.value,
            password:identifyingPasswordInput[0].value,
            rePassword:identifyingPasswordInput[1].value,
            qqEmail:emailLoginInputText.value
        },
        success:function(data){
            if(data.msg!="注册成功"){
                Tips(data.msg);
            }else{
                Tips("注册成功");
                loginInputContainer.style.display="none";
                emailLogin.style.display="block";
                loginBtnContainer.style.left="100%";
                handoverSign.style.right="100%";
                identifyingPasswordInput[0].value="";
                identifyingPasswordInput[1].value="";
                identifyingInput.value="";
                emailLoginInputText.value="";
                signTrue.value="";
                emailLoginInputText.disabled='true';
                setTimeout(function(){
                    handoverLogin.style.left="0";
                    singBtnContainer.style.right="0";
                },300);
            }
        }
    })
}
emailLoginInputText.onfocus=function(){
    emailLoginInputText.style.border="";
}
for(let i=0;i<2;i++){
    identifyingPasswordInput[i].onfocus=function(){
        identifyingPasswordInput[0].style.border="";
        identifyingPasswordInput[1].style.border="";
    }
}
/*其他方式登录*/
/*邮箱登录输入框*/
let emailLoginInputTextT =document.getElementsByClassName("emailLoginInputTextT")[0];
/*验证码输入框*/
let VerificationCode=document.getElementsByClassName("VerificationCode")[0];

valiBtn[0].onclick=function() {
    if (!QQ.test(emailLoginInputTextT.value)) {
        Tips("邮箱格式不正确");
    } else {
        $.ajax({
            type: 'post',
            url: '../email/sendEmail',
            data: {
                qqEmail: emailLoginInputTextT.value,
            },
            success: function (data) {
                emailLoginT();
            }
        })
    }
}
function emailLoginT(){
    emailLoginBtn[0].onclick=function(){
        if(emailLoginInputTextT.value==""){
            Tips("请输入QQ邮箱号");
        }else
        if(VerificationCode==""){
            Tips("请输入验证码");
        }else {
            $.ajax({
                type:'post',
                url:'../email/emailLogin',
                data:{
                    qqEmail: emailLoginInputTextT.value,
                    emailCode:VerificationCode.value
                },
                success:function(data){
                    if(data.msg=="验证码错误"){
                        Tips("验证码错误");
                    }else if(data.msg=="用户名不存在"){
                        Tips("用户名不存在");
                    }else{
                        sessionStorage.setItem("account",data.data.account);
                        sessionStorage.setItem("age",data.data.age);
                        sessionStorage.setItem("birthday",data.data.birthday);
                        sessionStorage.setItem("src",data.data.src);
                        sessionStorage.setItem("email",data.data.email);
                        sessionStorage.setItem("id",data.data.id);
                        sessionStorage.setItem("nickname",data.data.nickname);
                        sessionStorage.setItem("password",data.data.password);
                        sessionStorage.setItem("realname",data.data.realname);
                        sessionStorage.setItem("registTime",data.data.registTime);
                        sessionStorage.setItem("sex",data.data.sex);
                        sessionStorage.setItem("src",data.data.src);
                        sessionStorage.setItem("vip",data.data.vip);
                        sessionStorage.setItem("telephone",data.data.telephone);
                        userName.style.border="1px solid green";
                        password.style.border="1px solid green";
                        Tips("登录成功");
                        if(data.data.nickname==null){
                            setTimeout(function(){
                                window.location.href="../to/toPerfect";
                            },1000)
                        }else{
                            setTimeout(function(){
                                window.location.href="../tooIndex";
                            },1000)
                        }
                    }
                }
            });
        }
    }
}
let userName=document.getElementsByClassName("userName")[0];
let password=document.getElementsByClassName("password")[0];
let login_btn=document.getElementsByClassName("login_btn")[0];
let error=document.getElementsByClassName("error");
let tips=document.getElementsByClassName("tips")[0];
/*登录*/
function Login(){
    $.ajax({
        type:'post',
        url:"../user/loginAccount",
        async:true,
        data:{
            account:userName.value,
            password:password.value,
        },
        success:function(data){
            if(data.code==0){
                    sessionStorage.setItem("account",data.data.account);
                    sessionStorage.setItem("age",data.data.age);
                    sessionStorage.setItem("birthday",data.data.birthday);
                    sessionStorage.setItem("src",data.data.src);
                    sessionStorage.setItem("email",data.data.email);
                    sessionStorage.setItem("id",data.data.id);
                    sessionStorage.setItem("nickname",data.data.nickname);
                    sessionStorage.setItem("password",data.data.password);
                    sessionStorage.setItem("realname",data.data.realname);
                    sessionStorage.setItem("registTime",data.data.registTime);
                    sessionStorage.setItem("sex",data.data.sex);
                    sessionStorage.setItem("src",data.data.src);
                    sessionStorage.setItem("vip",data.data.vip);
                    sessionStorage.setItem("telephone",data.data.telephone);
                    userName.style.border="1px solid green";
                    password.style.border="1px solid green";
                    Tips("登录成功");
                    if(data.data.nickname==null){
                        setTimeout(function(){
                            window.location.href="../to/toPerfect";
                        },1000)
                    }else{
                        setTimeout(function(){
                            window.location.href="../tooIndex";
                        },1000)
                    }
            }else{
                userName.style.border="1px solid red";
                password.style.border="1px solid red";
                Tips("账号或密码错误");
            }
        }
    });
}
login_btn.onclick=function(){
    Login();
}

/*注册*/
var signuserName=document.getElementsByClassName("signuserName")[0];
var signpassword=document.getElementsByClassName("signpassword");
function Sign(){
    $.ajax({
        type:'post',
        url:"../user/insertUser",
        async:true,
        data:{
            account:signuserName.value,
            password:signpassword[0].value,
            repassword:signpassword[1].value
        },
        success:function(data){
            if(data.msg=="用户名已存在"){
                Tips("用户名已存在");
            }else{
                Tips("注册成功");
                loginBtnContainer.style.left="100%";
                handoverSign.style.right="100%";
                signuserName.value="";
                signpassword[0].value="";
                signpassword[1].value="";
                signTrue.value="";
                setTimeout(function(){
                    handoverLogin.style.left="0";
                    singBtnContainer.style.right="0";
                },300);
            }
        }
    });
}
signpassword[0].onfocus=function(){
    signpassword[0].style.border="";
    signpassword[1].style.border="";
}
signpassword[1].onfocus=function(){
    signpassword[0].style.border="";
    signpassword[1].style.border="";
}
function Tips(i){
    tips.innerHTML=i;
    tips.style.top="56px";
    let timer=setTimeout(function(){
        tips.style.top="-56px";
        clearTimeout(timer);
    },1000);
}
